#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int max = -1000;
    int a;
    cin>>a;
    int massiv[10] = {-1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000};
    if(a >= 1000 and a < 10000){
    int n1 = a/1000;
    int n2 = a/100%10;
    int n3 = a%100/10;
    int n4 = a%10;
    int c1 = 10*n1+n2;
    int c2 = 10*n2+n3;
    int c3 = 10*n3+n4;
    int c4=100*n1+10*n2+n3;
    int c5 = 100*n2+10*n3+n4;
    int allmass[10]={a,n1,n2,n3,n4,c1,c2,c3,c4,c5};
    for(int i = 2; i<sqrt(a)+1; i++){
        if(a%i==0){
            massiv[0]=a;
            break;
        }
    }
    for(int i = 2; i<sqrt(n1)+1; i++){
        if(n1%i==0 && n1 != 2){
            massiv[1]=n1;
            break;
        }
    }
    for(int i = 2; i<sqrt(n2)+1; i++){
        if(n2%i==0 && n2 != 2){
            massiv[2]=n2;
            break;
        }
    }
    for(int i = 2; i<sqrt(n3)+1; i++){
        if(n3%i==0 && n3 != 2){
            massiv[3]=n3;
            break;
        }
    }
    for(int i = 2; i<sqrt(n4)+1; i++){
        if(n4%i==0 && n4 != 2){
            massiv[4]=n4;
            break;
        }
    }
    for(int i = 2; i<sqrt(c1)+1; i++){
        if(c1%i==0){
            massiv[5]=c1;
            break;
        }
    }
    for(int i = 2; i<sqrt(c2)+1; i++){
        if(c2%i==0){
            massiv[6]=c2;
            break;
        }
    }
    for(int i = 2; i<sqrt(c3)+1; i++){
        if(c3%i==0){
            massiv[7]=c3;
            break;
        }
    }
    for(int i = 2; i<sqrt(c4)+1; i++){
        if(c4%i==0){
            massiv[8]=c4;
            break;
        }
    }
    for(int i = 2; i<sqrt(c5)+1; i++){
        if(c5%i==0){
            massiv[9]=c5;
            break;
        }
    }
    int goodmassiv[10]={0,0,0,0,0,0,0,0,0,0};
    for(int i = 0; i<10; i++){
        if(allmass[i] != massiv[i]){
            goodmassiv[i]=allmass[i];
        }
    }
    for(int i: goodmassiv){
        if(i>=max && i!=1)
            max=i;
    }
    cout << max;
}
if(a<1000 or a >=10000){
    cout << 0;
}
}